package aufgabe2;

import java.util.ArrayList;

/**
 * Represents the Connection Pool
 * 
 */

public class ConnectionPool {

	private static final int MAX_CONNECTIONS = 5;
	private static ArrayList<DBConnection> _conns = new ArrayList<DBConnection>();

	/**
	 * Constructot which inializes Connections
	 */
	private ConnectionPool() {
		init();
	}

	public static void init() {
		for (int i = 0; i < MAX_CONNECTIONS; i++) {
			_conns.add(new DBConnection());
		}
	}

	/**
	 * Adds a Conn. and checks if max_size is reached
	 * 
	 * @return
	 */
	public static String addConnection() {
		if (_conns.size() < MAX_CONNECTIONS) {
			_conns.add(new DBConnection());
			return "" + getConnections();
		} else
			return "Connection not set, actual " + getConnections();

	}

	public static void cancelConnection() {
		_conns.remove(_conns.remove(0));
	}

	/**
	 * returns unset Connections
	 * 
	 * @return
	 */
	public static int getFreeSlots() {
		return MAX_CONNECTIONS - _conns.size();
	}

	public static String getConnections() {
		return _conns.size() + " set, and " + getFreeSlots() + " free";

	}
}
